<?php
/* -------------------------------------------------------------------------------------
* 	ID:						$Id: weather.php 11 2013-09-21 11:43:11Z phone.mueller@googlemail.com $
* 	Letzter Stand:			$Revision: 11 $
* 	zuletzt geaendert von:	$Author: peter $
* 	Datum:					$Date: 2013-09-21 11:43:11 +0000 (Sat, 21 Sep 2013) $
*
* 	SEO:mercari by Siekiera Media
* 	http://www.seo-mercari.de
*
* 	Copyright (c) since 2011 SEO:mercari
* --------------------------------------------------------------------------------------
* 	based on:
* 	(c) 2000-2001 The Exchange Project  (earlier name of osCommerce)
* 	(c) 2002-2003 osCommerce - www.oscommerce.com
* 	(c) 2003     nextcommerce - www.nextcommerce.org
* 	(c) 2005     xt:Commerce - www.xt-commerce.com
*
* 	Released under the GNU General Public License
* ----------------------------------------------------------------------------------- */

function website_wetter($plz, $land, $icons_src="/", $sprache="de", $ort="") {
	$icons_google = "/ig/images/weather/";

	if($ort != "")
		$station = $ort;
	else
		$station = $plz."-".$land;
	
	if(function_exists('curl_init')) {
		$url = 'http://www.google.com/ig/api?weather='.$station.'&hl='.$sprache;
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, $url);
		curl_setopt($ch, CURLOPT_TIMEOUT, 10);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
		$content = curl_exec($ch);
		curl_close($ch);
	
	} elseif(ini_get(allow_url_fopen) !== false && function_exists(file_get_contents)) {
		$content = file_get_contents("http://www.google.com/ig/api?weather=".$station."&hl=".$sprache);
		
	} else
		return;
	
	$api = simplexml_load_string(utf8_encode($content));

	$wetter = array();

	if(!is_object($api))
		return false;
	// Allgemeine Informationen
	$wetter['stadt'] = $api->weather->forecast_information->city->attributes()->data;
	$wetter['datum'] = $api->weather->forecast_information->forecast_date->attributes()->data;
	$wetter['zeit'] = $api->weather->forecast_information->current_date_time->attributes()->data;

	// Aktuelles Wetter
	$wetter[0]['zustand'] = $api->weather->current_conditions->condition->attributes()->data;
	$wetter[0]['temperatur'] = $api->weather->current_conditions->temp_c->attributes()->data;
	$wetter[0]['luftfeuchtigkeit'] = $api->weather->current_conditions->humidity->attributes()->data;
	$wetter[0]['wind'] = $api->weather->current_conditions->wind_condition->attributes()->data;
	$wetter[0]['icon'] = str_replace($icons_google, $icons_src, $api->weather->current_conditions->icon->attributes()->data);

	// Wettervorhersage heute, morgen, in zwei und in drei Tagen ($wetter[1] bis $wetter[4])
	$i = 1;
	foreach($api->weather->forecast_conditions as $weather) {
		$wetter[$i]['wochentag'] = $weather->day_of_week->attributes()->data;
		$wetter[$i]['zustand'] = $weather->condition->attributes()->data;
		$wetter[$i]['tiefsttemperatur'] = $weather->low->attributes()->data;
		$wetter[$i]['hoechsttemperatur'] = $weather->high->attributes()->data;
		$wetter[$i]['icon'] = str_replace($icons_google, $icons_src, $weather->icon->attributes()->data);

		$i++;
	}

	return $wetter;
}

$data = $db->db_query("SELECT
							ab.entry_postcode,
							ct.countries_name
						FROM
							".TABLE_ADDRESS_BOOK." ab,
							".TABLE_COUNTRIES." ct,
							".TABLE_CUSTOMERS." c
						WHERE
							c.customers_id = '".$_SESSION['customer_id']."'
						AND
							ab.address_book_id = c.customers_default_address_id
						AND
							ct.countries_id = ab.entry_country_id");

$wetter = website_wetter($data->fields['entry_postcode'], $data->fields['countries_name']);
if(!empty($wetter)) {
	echo '	<table width="100%" class="dataTable">';
	
	echo '		<tr><td colspan="2"><b>'.$wetter['stadt'].'</b></td></tr>';
	
	echo '		<tr>';
	echo '			<td width="1"><img src="https://www.google.com/ig/images/weather'.$wetter[0]['icon'].'" alt="'.$wetter[0]['zustand'].'" /></td>';
	echo '			<td>
						<strong>Aktuell:</strong><br/>
						'.$wetter[0]['zustand'].' / Temperatur: '.$wetter[0]['temperatur'].'&deg; C / '.$wetter[0]['luftfeuchtigkeit'].' / '.$wetter[0]['wind'] .'
					</td>';
	echo '		</tr>';
	
	for($i = 1; $i < 5; $i++) {
		echo '		<tr>';
		echo '			<td><img src="https://www.google.com/ig/images/weather'.$wetter[$i]['icon'].'" alt="'.$wetter[$i]['zustand'].'" /></td>';
		echo '			<td>
							'.$wetter[$i]['wochentag'].' - '.$wetter[$i]['zustand'].'<br />
							min. '.$wetter[$i]['tiefsttemperatur'].'&deg; C | max. '.$wetter[$i]['hoechsttemperatur'].'&deg; C
						</td>';
		echo '		</tr>';
	}
	
	echo '</table>';
} else 
	echo '<em>Die Wetterdaten für '.$data->fields['entry_postcode'].' in '.$data->fields['countries_name'].' konnten nicht geladen werden.</em>';
?>